package edu.nyu.hps.assignment5;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public abstract class Voronoi {
	static int player, stones, time;
	double opponentArea, myArea, myMyArea, myOpponentArea, diameter;
	static Point[][] grid = new Point[501][501];
	Point center = null;
	static List<Point> opponentStones = new ArrayList<Point>();
	static List<Point> myStones = new ArrayList<Point>();

	/**
	 * Updates the closest stone for every point on the grid
	 */
	public void updateClosestStone() {
		for (int i = 0; i < 501; i++) {
			for (int j = 0; j < 501; j++) {
				if (!grid[i][j].taken) {
					double closestDistance;
					if (grid[i][j].closestStone != null) {
						closestDistance = grid[i][j]
								.dist(grid[i][j].closestStone);
					} else {
						closestDistance = 251002;
					}
					// Updating closestStones based on opponent stones
					for (int k = 0; k < opponentStones.size(); k++) {
						double tempDistance = grid[i][j].dist(opponentStones
								.get(k));
						if (closestDistance > tempDistance) {
							closestDistance = tempDistance;
							grid[i][j].closestStone = opponentStones.get(k);
							grid[i][j].takenBy = player ^ 1;
						}

						// else if(closestDistance == tempDistance &&
						// grid[i][j].takenBy == player){
						// closestDistance = 0;
						// grid[i][j].closestStone = grid[i][j];
						// grid[i][j].takenBy = -1;
						// }
					}

					// Updating closestStones based on my stones
					for (int l = 0; l < myStones.size(); l++) {
						double tempDistance = grid[i][j].dist(myStones.get(l));
						if (closestDistance > tempDistance) {
							closestDistance = tempDistance;
							grid[i][j].closestStone = opponentStones.get(l);
							grid[i][j].takenBy = player;
						}

						// else if(closestDistance == tempDistance &&
						// grid[i][j].takenBy == (player^1)){
						// closestDistance = 0;
						// grid[i][j].closestStone = grid[i][j];
						// grid[i][j].takenBy = -1;
						// }
					}
				}
			}
		}
	}

	public void updatesBeforeComputing() {
		updateClosestStone();
		updateArea();
	}

	/**
	 * Returns a point where the stone should be thrown
	 * 
	 * @return
	 */
	public abstract Point throwAt();

	/**
	 * Updates the values of total areas
	 */
	public abstract void updateArea();
	
	
	/**
	 * Extra part for the visualizer..
	 * TBR
	 */
	public void createImage() {
		BufferedImage image = new BufferedImage(501, 501,
				BufferedImage.TYPE_3BYTE_BGR);
		int rgb;
		for (int i = 0; i < 501; i++) {
			for (int j = 0; j < 501; j++) {
				if (!grid[i][j].taken) {
					if (grid[i][j].takenBy == 0) {
						rgb = Color.red.getRGB();
					} else if (grid[i][j].takenBy == 1) {
						rgb = Color.blue.getRGB();
					} else {
						rgb = Color.white.getRGB();
					}
				} else {
					if(grid[i][j].takenBy == 0){
						rgb = Color.black.getRGB();
					}
					else if (grid[i][j].takenBy == 1){
						rgb = Color.white.getRGB();
					}
					else{
						rgb = Color.pink.getRGB();
					}
					for (int a = i - 2; a <= i + 2; a++) {
						for (int k = j - 2; k <= j + 2; k++) {
							if (a >= 0 && a < 501 && k >= 0 && k < 501) {
								image.setRGB(a, k, rgb);
							}
						}
					}
				}
				image.setRGB(i, j, rgb);

			}
		}
		File f = new File(
				"C:\\Users\\Rohan\\workspace\\myheuristics\\image\\myimage"
						+ Util.imageid++ + ".jpg");
		try {
			ImageIO.write(image, "jpg", f);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
